package cn.wolfcode.wolf2w.redis.impl;

import cn.wolfcode.wolf2w.redis.IPersonalRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class PersonalRedisServiceImpl implements IPersonalRedisService{
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public void AllVistitorAdd(String uid, String vid) {
        Integer num = 0;
        if(redisTemplate.hasKey("AllVistitorsNums:"+uid)){
            num =  (int)redisTemplate.opsForValue().get("AllVistitorsNums:"+uid);
        }else{
            redisTemplate.opsForValue().set("AllVistitorsNums:"+uid,num+1);
        }
    }

    @Override
    public void OneDayVistitorAdd(String uid, String vid) {
        Integer num = 0;
        System.out.println(redisTemplate.hasKey("OneDayVistitorNums:" + uid));
        System.out.println(redisTemplate.opsForValue().get("OneDayVistitorNums:" + uid));
        if(redisTemplate.hasKey("OneDayVistitorNums:"+uid)){
            num =  (Integer) redisTemplate.opsForValue().get("OneDayVistitorNums:"+uid);
        }else{
            redisTemplate.opsForValue().set("OneDayVistitorNums:"+uid,num+1,86400, TimeUnit.SECONDS);
        }
    }
    public Integer getAllDayTimes(String uid, String vid){
        //如果存在可以  返回  不存在返回0
        if(redisTemplate.hasKey("AllVistitorsNums:"+uid)) {
            return (Integer) redisTemplate.opsForValue().get("AllVistitorsNums:"+uid);
        }
      return 0;
    }
    public Integer getOneDayVistitor(String uid, String vid){
        if(redisTemplate.hasKey("OneDayVistitorNums:"+uid)) {
            return (Integer) redisTemplate.opsForValue().get("OneDayVistitorNums:" + uid);
        }
        return 0;
    }
}
